package org.cornland.systemWarning.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {


    /**
     * 得到当前时间的String格式
     * @return
     */
    public static String getCurrentTimeFormat(){
        Date date = new Date();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String retVal = df.format(date);

        return retVal;
    }

    /**
     * 将其余格式的日期String统一转换为yyyy-MM-dd HH:mm:ss
     * @param time
     * @return
     * @throws ParseException
     */
    public static String dateFormat(String time) throws ParseException {
        // 去除两头
        String timeTrim = time.trim();
        // 获得前半与后半并且trim
        String fore = timeTrim.substring(0,timeTrim.indexOf(" "));
        String back = (timeTrim.substring(timeTrim.indexOf(" "))).trim();
        // 拼接
        String str = fore + " " + back;
        // 换成 yyyy-MM-dd HH:mm:ss
        str = str.replaceAll("/", "-");

        // 上一步可能不完全 如2021/11/3 14:49:28 再foramt一次
        DateFormat dateTimeInstance = DateFormat.getDateTimeInstance();
        Date parse = dateTimeInstance.parse(str);

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = df.format(parse);

        return format;
    }


    public static boolean inFiveMin(String time) throws ParseException {
        // 5分前的时间戳
        Calendar c1 = Calendar.getInstance();
        c1.add(Calendar.SECOND, -5*60);
        Date d1 = c1.getTime();

        // 5分后的时间戳
        Calendar c2 = Calendar.getInstance();
        c2.add(Calendar.SECOND, 5*60);
        Date d2 = c2.getTime();

        // 控制器的Date
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = df.parse(time);

        if(d.getTime() >= d1.getTime() && d.getTime() <= d2.getTime()){
            return true;
        }else{
            return false;
        }

    }


    /**
     * 将时间String转换为带空格(%20)格式
     * @param s
     * @throws ParseException
     */
    public static String transferTimeBlank(String s) throws ParseException {
        //此处重新转一遍date防止出错
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d1 = df.parse(s);
        String retVal = df.format(d1);
        //替换blank为%20
        String newString = retVal.replaceAll(" ", "%20");
        return newString;
    }

    /**
     * 将给定的时间string加上指定的分钟数
     * @param s1
     * @param diff
     * @return
     * @throws ParseException
     */
    public static String getNewTimeByAddingMin(String s1, Integer diff) throws ParseException {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // String -> Date
        Date d1 = df.parse(s1);

        // new calender
        Calendar newTime = Calendar.getInstance();
        // add a date
        newTime.setTime(d1);
        // count time
        newTime.add(Calendar.SECOND, diff*60);//日期加diff秒

        // Calender -> date
        Date dt1=newTime.getTime();
        // date -> String
        String retVal = df.format(dt1);

        return retVal;

    }


    public static String formatTime(Long ms) {
        Integer ss = 1;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;

        StringBuffer sb = new StringBuffer();
        if(day > 0) {
            sb.append(day+"天");
        }
        if(hour > 0) {
            sb.append(hour+"小时");
        }
        if(minute > 0) {
            sb.append(minute+"分");
        }
        if(second > 0) {
            sb.append(second+"秒");
        }
        return sb.toString();
    }

    public static String formatTimeMin(Long min) {
        Integer hh = 60;

        Long hour = min / hh;
        Long minute = min - hour * hh;

        StringBuffer sb = new StringBuffer();

        if(hour > 0) {
            sb.append(hour+"小时");
        }
        if(minute > 0) {
            sb.append(minute+"分钟");
        }
        return sb.toString();
    }
    /**
     * 判断当前时间是否在时间段内
     * @param begin  开始时间字符串
     * @param end    结束时间字符串
     * @param time   当前时间字符串
     * @return
     */
    public static boolean compareTime(String begin, String end, String time) {
        boolean result = false;
        //将时间字符串转化成时间
        //String转Date
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            //转换成时间格式
            Date beginTime = df.parse(begin);
            Date endTime = df.parse(end);
            //取出当前时间的时分秒编码再解码
            Date date = df.parse(time);
            //通过日历形式开始比较
            Calendar b = Calendar.getInstance();
            b.setTime(beginTime);
            Calendar e = Calendar.getInstance();
            e.setTime(endTime);
            Calendar d = Calendar.getInstance();
            d.setTime(date);
            //当前时间晚于开始时间，早于结束时间则表明在指定的时间段内
            if (d.after(b) && d.before(e)) {
                result = true;
            }
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
        return result;
    }
}
